<template>
	<el-dialog v-model="dialogVisible" title="素材管理" width="1100px">
		<template #header>
			<div style="display: flex;flex-direction: row;">
				<div style="flex: 1;">
					<el-radio-group size="default" v-model="medieType">
						<el-radio-button label="全部" value="" />
						<el-radio-button label="图片" value="image" />
						<el-radio-button label="视频" value="video" />
						<el-radio-button label="文件" value="file" />
					</el-radio-group>
				</div>
				<div
					style="flex: 1; display: flex; align-items: center;justify-content: center; font-size: 17px; font-weight: bold;">
					素材管理
				</div>
				<div style="flex: 1;">

				</div>
			</div>
			<el-divider style="margin-bottom: 0px;" v-if="false" />
		</template>


		<el-container style="height: 500px;">
			<el-aside width="240px">
				<blCategoryTree model="Media" title="素材分类" ref="group" @node-click="groupClick" />
			</el-aside>

			<el-container>

				<el-header>
					<div class="left-panel">
						<el-button type="primary" @click="handleQuanXuan">全选</el-button>
						<el-button type="primary" icon="el-icon-plus" @click="handleUpload">上传素材</el-button>
					</div>
					<div class="right-panel">
						<div class="right-panel-search">
							<el-input v-model="search.keyword" placeholder="素材名称" clearable></el-input>
							<el-button type="primary" icon="el-icon-search" @click="upsearch">查询</el-button>
						</div>
					</div>
				</el-header>
				<el-main class="nopadding">
					<taTableCard :params="params" hideAdd rowHeight="75px" :cardSize="[8,8,8,12,24]" ref="table"
						:apiObj="apiObj" row-key="id">
						<template #default="{item}">
							<div :class="[item.$selected?'mediaSelect':'']" @click="item.$selected=!item.$selected"
								style="display: flex;flex-direction: row; justify-content: space-between; position: relative;">
								<template v-if="item.type=='image'">
									<div style="height: 35px;">
										<img style="max-width: 35px; max-height: 35px; object-fit: contain;"
											:src="item.src+'?x-oss-process=image/resize,w_35&t='" :alt="item.title" />
									</div>
								</template>
								<template v-if="item.type=='video'">
									<el-image style="max-width: 35px; max-height: 35px;" preview-teleported
										hide-on-click-modal :src="item.src+'?x-oss-process=video/snapshot,t_0,f_jpg'"
										:preview-src-list="[item.src+'?x-oss-process=video/snapshot,t_0,f_jpg']"
										show-progress fit="contain" :z-index="19900501" :alt="item.title" />
								</template>
								<div style="padding-left: 15px;text-align: right;">
									<el-text style="font-size: 14px; width: 165px;" truncated>{{item.title}}</el-text>
									<p style="color: #606266;">{{item.created_at}}</p>
								</div>
								<el-dropdown class="media-dropdown" trigger="click">
									<el-button size="small" icon="ArrowDown" @click.stop="" />
									<template #dropdown>
										<el-dropdown-menu>
											<el-dropdown-item>
												<el-button size="small" type="success" icon="Link"
													@click="handleCopy(item.src)">
													链接
												</el-button>
											</el-dropdown-item>
											<el-dropdown-item>
												<el-button size="small" type="primary" icon="Edit"
													@click="table_edit(item)">
													编辑
												</el-button>
											</el-dropdown-item>
											<el-dropdown-item>
												<el-button size="small" type="danger" icon="Delete" @click.stop="">
													删除
												</el-button>
											</el-dropdown-item>
										</el-dropdown-menu>
									</template>
								</el-dropdown>


							</div>
						</template>
					</taTableCard>
				</el-main>
			</el-container>
		</el-container>

		<template #footer>
			<span class="dialog-footer">
				<el-button @click="dialogVisible = false">取消</el-button>
				<el-button type="primary" @click="handleQueRen">
					确认
				</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script>
	//import saveDialog from './save'
	//import uploadDialog from './upload'
	//import previewDialog from './save'
	import scSelectFilter from '@/components/scSelectFilter'

	export default {
		name: 'media.manage',
		components: {
			//	saveDialog,
			//	uploadDialog,
			scSelectFilter
		},
		data() {
			return {
				params: {},
				cb: null,
				medieType: '',
				dialogVisible: false,
				apiObj: this.$API.system.media.list,
				selection: [],
				search: {
					keyword: null,
					dateRange: null
				}
			}
		},
		methods: {
			handleQueRen() {
				this.dialogVisible = false;
				this.cb && this.cb(this.$refs.table.selectedRows)
			},
			open(op, cb) {
				this.params = {
					selectFilter: {
						type: op.type
					}
				}
				if (op.type) {
					this.medieType = op.type;
				}

				this.cb = cb;
				this.dialogVisible = true;
				this.$nextTick(() => {
					this.$refs.table.upData()
				})
			},
			// 上传
			handleUpload() {
				alert('开发中')
			},
			handleQuanXuan() {
				this.$refs.table.toggleAllSelection()
			},
			async handleCopy(text) {
				try {
					await navigator.clipboard.writeText(text);
					this.$message.success("复制成功！");
				} catch (err) {
					this.$message.error("复制失败，请手动复制");
				}
			},
			stateChange(e) {
				var params = {
					selectFilter: e
				}
				this.$refs.table.upData(params)
			},
			// 上传素材
			upload() {
				this.dialog.upload = true;
				this.$nextTick(() => {
					const checkedNode = this.$refs.group.getCurrentNode();
					this.$refs.uploadDialog.open().setData({
						category_id: checkedNode.id || null
					});
				})
			},
			table_preview(row) {
				this.dialog.preview = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('preview').setData(row)
				})
			},
			//编辑
			table_edit(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row);
				})
			},
			//查看
			table_show(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row)
				})
			},
			//删除
			async table_del(row, index) {

				return this.$alert("开发中...", "提示", {
					type: 'error'
				});

				var reqData = {
					id: row.id
				}
				var res = await this.$API.system.media.remove.post(reqData);
				if (res.code == 0) {
					this.$refs.table.tableData.splice(index, 1);
					this.$message.success(res.msg)
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//批量删除
			async batch_del() {
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					this.selection.forEach(item => {
						this.$refs.table.tableData.forEach((itemI, indexI) => {
							if (item.id === itemI.id) {
								this.$refs.table.tableData.splice(indexI, 1)
							}
						})
					})
					loading.close();
					this.$message.success("操作成功")
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection) {
				this.selection = selection;
			},
			//树点击事件
			groupClick(data) {
				var params = {
					category_id: data.id
				}
				this.$refs.table.upData(params)
			},
			//搜索
			upsearch() {
				this.$refs.table.upData(this.search)
			},
			//本地更新数据
			handleSuccess(data, mode) {
				if (mode == 'add') {
					this.$refs.table.refresh()
				} else if (mode == 'edit') {
					this.$refs.table.refresh()
				}
			}
		}
	}
</script>

<style scoped>
	:deep(.el-main) {
		padding-bottom: 0px !important;
	}

	.media-dropdown {
		position: absolute;
		right: -17px;
		bottom: -10px;
		display: none;
	}

	:deep(.el-card__body:hover) .media-dropdown {
		display: block;
	}

	:deep(.el-card:has(.mediaSelect)) {
		border: 2px solid #409eff;
	}
</style>
